Introduction to Bash Scripting

What is Bash scripting ?

Bash scripting is the method of automating bash/terminal commands.

lets understand bash scripting by building a ping sweeper:

  • usually when we run ping command ping 192.168.x.x it gives this output, by sending icmp packet.
  • if the system receives icmp packet back it means the other system is alive/connected to the same network.
PING 192.168.3.2 (192.168.3.2) 56(84) bytes of data.
64 bytes from 192.168.3.2: icmp_seq=1 ttl=128 time=0.929 ms
^C
--- 192.168.3.2 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.929/0.929/0.929/0.000 ms

  • as u can see down below this is bash script that check if the system is alive by sending icmp packet
#!/bin/bash
if [ "$1" == "" ]; then # if $1 == null --> this passes anrgument and arguments are written by the user
  echo "Type the IP Address!"
  echo "This is the syntax: ./filename.sh 192.168.1"
else # if $1 is not null it will execute the ping command 
  for ip in `seq 1 254`; do
    ping -c 1 $1.$ip | grep "64 bytes" | cut -d " " -f 4 | tr -d ":" &
  done
fi # ending the statement

bash scripting is about automating bash/terminal commands as a single script.

  • #!/bin/bash the shebang, this tells the interpreter to execute as a bash file .sh
  • like in any programming Language we can use if/else statement to preform conditions.
  • In the else we made a for loop that check the IP subnet seq 1 254 of /24 prefix 192.168.1.x
  • by using pipe | we combine the ping commands and formatting methods and filtering this output:
    • 64 bytes from 192.168.3.2: icmp_seq=1 ttl=128 time=0.929 ms
    • grep "64 bytes" this grabs the line as shown above
    • then we use cut -d "space" -f 4 to cut the delimiter till it finds the ipv4 and filter the ping response. this what remains after cutting 192.168.2.2: then we use tr -d ":" = translate to remove the ":".
    • Pasted image 20250622222331.png
    • then we are left we a clean ipv4 address example `192.168.1.1
  • Now the output of the for loop will give u the IP's of alive system on your network
  • run this commands to execute the code.
  • $1 is a paremeter will be taken by You who is running the script its like providing input in python
sudo chmod +x # assuming that the file is not executable.
./pingsweeper 192.168.1 # the $1 value --> specify your ip subnet.

Creating a Bash script for setting up my kali VM After installing:

#!/bin/bash
echo "starting Mysetup"
konsole &
sleep 2
firefox &
sleep 2
zettlr &
sleep 2
flameshot &

Shell Scripting(Bash Scripting):

Basic Script:

echo "hey,what's Your "
read name
echo "welcome,$name"

Loops:

for i in {1..10};
do
echo $i
done

Conditional Statements in Bash + Summary:

echo "Hey What is your Name"
read name
echo "Welcome,$name"

echo "Pleae enter Your name Alias:"
read name2

if [ "$name2" = "meezok" ]; then
        echo "Welcome $name : Here is Temp login Password : IloveYou"
else
        echo "Sorry! You are not Authorized To Access"
        ip a
fi

for i in {1..10};
do
echo $i
done

Script Challenge:(Safe Locker ):

#!/bin/bash

echo "-----ENTER YOUR CREDITENTAILS To Unlock THE VAULT-----"
username=""
company=""
pin=""

for i in {1..3}; do
# Defining the conditional statements
        if [ "$i" -eq 1 ]; then
                echo "Enter your Username:"
                read username
        elif [ "$i" -eq 2 ]; then
                echo "Enter your Company name:"
                read companyname
        else
                echo "Enter your PIN:"
                read pin
        fi
done

# Checking if the user entered the correct details
if [ "$username" = "meezok" ] && [ "$company" = "utas" ] && [ "$pin" = "4321" ]; then
        echo "Authentication Successful. You can now access your locker, John."
else
        echo "Authentication Denied!!"
fi